/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.QrCodeService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.controller.common.BaseController;
import com.sigmatrix.h5.web.vo.QRCodeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *@ClassName: QRCodeController
 *@Description: 一级码、多级码相关接口控制器
 *@author youjun
 *@date 2017年6月28日 上午10:40:29
 */
//@ApiIgnore
@Api(tags="出入库查询码接口")
@RestController
@RequestMapping("/app/qrcode")
public class QRCodeController extends BaseController {

	/**logger record*/
	private static final Logger LOGGER = LoggerFactory.getLogger(QRCodeController.class);
	
	/**码信息业务层接口*/
	@Autowired
	private transient QrCodeService qRCodeService;
	
	/**
	 *@Title: queryCode
	 *@Description: 出入库查询码接口
	 *@return Response
	 *@author youjun
	 *@date 2017年6月28日 下午1:49:45
	 *@param request
	 *@param qRCodeVo
	 *@param result
	 *@return
	 */
	@ApiOperation(value = "修改出入库单查询码")
	@PostMapping("/update/querycode")
	public Response queryCodeForUpdateOutOrInStorage(
			@Valid @RequestBody final QRCodeVo qRCodeVo, final BindingResult result) {
		
		final int operationType = qRCodeVo.getOperationType()==null?Cons.QR_CODE_OPERATION_TYPE_OUT_STORAGE:qRCodeVo.getOperationType();
		final String operationTypeDesc = operationType==1?"出库":"入库";
		LOGGER.info("已存在的{}单查询码接口开始...", operationTypeDesc);
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("已存在的{}查询码接口参数验证失败{}", operationTypeDesc, errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			if(qRCodeVo.getCodeType()==null){
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, "码类型不能为空");
			}
			LOGGER.info("请求参数为:{}", JSONObject.toJSONString(qRCodeVo));
			return qRCodeService.queryCodeForUpdateOutOrInStorage(qRCodeVo);
		}catch(Exception e) {
			LOGGER.error("已存在的{}查询码接口失败", operationTypeDesc, e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
	/**
	 *@Title: queryCodeForAddOutOrInStorage
	 *@Description: 新增出入库单操作时查询码接口
	 *@return Response
	 *@author youjun
	 *@date 2017年7月3日 下午8:53:52
	 *@param request
	 *@param qRCodeVo
	 *@param result
	 *@return
	 */
	@ApiOperation(value = "新增出入库单查询码")
	@PostMapping("/add/querycode")
	public Response queryCodeForAddOutOrInStorage(
			@Valid @RequestBody final QRCodeVo qRCodeVo, final BindingResult result) {
		
		final int operationType = qRCodeVo.getOperationType()==null?Cons.QR_CODE_OPERATION_TYPE_OUT_STORAGE:qRCodeVo.getOperationType();
		final String operationTypeDesc = operationType==1?"出库":"入库";
		LOGGER.info("新增{}单查询码接口开始...", operationTypeDesc);
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("新增{}查询码接口参数验证失败{}", operationTypeDesc, errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			LOGGER.info("请求参数为:{}", JSONObject.toJSONString(qRCodeVo));
			final Response resultResponse = qRCodeService.queryCodeForAddOutOrInStorage(qRCodeVo);
			LOGGER.info("新增{}查询码接口参数验证失败", operationTypeDesc, JSONObject.toJSONString(resultResponse));
			return resultResponse;
		}catch(Exception e) {
			LOGGER.error("新增{}查询码接口失败", operationTypeDesc, e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
	
}
